package homework1127;

public class TicketBox implements Runnable {
    private static int ticket = 100;
    String t_name;

    public TicketBox(String t_name) {
        this.t_name = t_name;
    }

    public void getTicket() {
        while(true){
            synchronized (TicketBox.class) {
                if(ticket > 0) {
                    String threadName = Thread.currentThread().getName();
                    System.out.println("线程" + threadName + ": " + t_name + "出售了一张票,剩余票数:" + ticket--);
                }
                else {
                    System.out.println("线程" + Thread.currentThread().getName() + ": 站点" + t_name + "票已售完");
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    public void run() {
        getTicket();
    }
}
